package com.tg.demo.web;

import com.github.pagehelper.PageInfo;
import com.tg.demo.common.BarView;
import com.tg.demo.common.ConstantUtil;
import com.tg.demo.common.Page;
import com.tg.demo.common.Result;
import com.tg.demo.entity.Score;
import com.tg.demo.mapper.ScoreMapper;
import com.tg.demo.service.ScoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * LifeController
 *
 * @author yuanjin
 * @date 2019/3/29
 */
@Controller
@Slf4j
@RequestMapping("/score")
public class ScoreController {

    @Resource
    private ScoreService scoreService;

    @Resource
    private ScoreMapper scoreMapper;

    /**
     * 跳转到列表页
     *
     * @param model the model
     * @return string string
     */
    @RequestMapping(value = "/gotoListPage", method = {RequestMethod.GET, RequestMethod.POST})
    public String gotoListPage(Model model) {
        return "/score/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Page list(Page page, Score score) {
        PageInfo<Score> info = scoreService.getListByPage(score, page.getPage(), page.getPageSize());
        Page result = new Page();
        result.setTotal(Integer.parseInt(info.getTotal() + ""));
        result.setRows(info.getList());
        return result;
    }

    @RequestMapping(value = "/DA", method = {RequestMethod.GET, RequestMethod.POST})
    public String da(Model model) {
        return "/score/view";
    }

    @RequestMapping("/getItemsByProfessionalRequired")
    @ResponseBody
    public Result getItemsByProfessionalRequired() {
        List<Double> items = scoreMapper.getItemsByProfessionalRequired();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"小于60分", "60-69分", "70-79分", "80-89分", "90分及以上"};
        for (String item : arr) {
            keys.add(item);
            Double target = null;
            if (item.equals("小于60分")) {
                target = 60.00;
            } else if (item.equals("60-69分")) {
                target = 70.00;
            } else if (item.equals("70-79分")) {
                target = 80.00;
            } else if (item.equals("80-89分")) {
                target = 90.00;
            } else if (item.equals("90分及以上")) {
                target = 100.00;
            }
            values.add(getCount(items, target));
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/getItemsByComputerApplicationFoundation")
    @ResponseBody
    public Result getItemsByComputerApplicationFoundation() {
        List<Double> items = scoreMapper.getItemsByComputerApplicationFoundation();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"小于60分", "60-69分", "70-79分", "80-89分", "90分及以上"};
        for (String item : arr) {
            keys.add(item);
            Double target = null;
            if (item.equals("小于60分")) {
                target = 60.00;
            } else if (item.equals("60-69分")) {
                target = 70.00;
            } else if (item.equals("70-79分")) {
                target = 80.00;
            } else if (item.equals("80-89分")) {
                target = 90.00;
            } else if (item.equals("90分及以上")) {
                target = 100.00;
            }
            values.add(getCount(items, target));
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/getItemsByEnglish")
    @ResponseBody
    public Result getItemsByEnglish() {
        List<Double> items = scoreMapper.getItemsByEnglish();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"小于60分", "60-69分", "70-79分", "80-89分", "90分及以上"};
        for (String item : arr) {
            keys.add(item);
            Double target = null;
            if (item.equals("小于60分")) {
                target = 60.00;
            } else if (item.equals("60-69分")) {
                target = 70.00;
            } else if (item.equals("70-79分")) {
                target = 80.00;
            } else if (item.equals("80-89分")) {
                target = 90.00;
            } else if (item.equals("90分及以上")) {
                target = 100.00;
            }
            values.add(getCount(items, target));
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/getItemsByMath")
    @ResponseBody
    public Result getItemsByMath() {
        List<Double> items = scoreMapper.getItemsByMath();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"小于60分", "60-69分", "70-79分", "80-89分", "90分及以上"};
        for (String item : arr) {
            keys.add(item);
            Double target = null;
            if (item.equals("小于60分")) {
                target = 60.00;
            } else if (item.equals("60-69分")) {
                target = 70.00;
            } else if (item.equals("70-79分")) {
                target = 80.00;
            } else if (item.equals("80-89分")) {
                target = 90.00;
            } else if (item.equals("90分及以上")) {
                target = 100.00;
            }
            values.add(getCount(items, target));
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/getItemsByProfessionalElective")
    @ResponseBody
    public Result getItemsByProfessionalElective() {
        List<Double> items = scoreMapper.getItemsByProfessionalElective();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"小于60分", "60-69分", "70-79分", "80-89分", "90分及以上"};
        for (String item : arr) {
            keys.add(item);
            Double target = null;
            if (item.equals("小于60分")) {
                target = 60.00;
            } else if (item.equals("60-69分")) {
                target = 70.00;
            } else if (item.equals("70-79分")) {
                target = 80.00;
            } else if (item.equals("80-89分")) {
                target = 90.00;
            } else if (item.equals("90分及以上")) {
                target = 100.00;
            }
            values.add(getCount(items, target));
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/getItemsByMarxism")
    @ResponseBody
    public Result getItemsByMarxism() {
        List<Double> items = scoreMapper.getItemsByMarxism();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"小于60分", "60-69分", "70-79分", "80-89分", "90分及以上"};
        for (String item : arr) {
            keys.add(item);
            Double target = null;
            if (item.equals("小于60分")) {
                target = 60.00;
            } else if (item.equals("60-69分")) {
                target = 70.00;
            } else if (item.equals("70-79分")) {
                target = 80.00;
            } else if (item.equals("80-89分")) {
                target = 90.00;
            } else if (item.equals("90分及以上")) {
                target = 100.00;
            }
            values.add(getCount(items, target));
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/getItemsByChinaSpecial")
    @ResponseBody
    public Result getItemsByChinaSpecial() {
        List<Double> items = scoreMapper.getItemsByChinaSpecial();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"小于60分", "60-69分", "70-79分", "80-89分", "90分及以上"};
        for (String item : arr) {
            keys.add(item);
            Double target = null;
            if (item.equals("小于60分")) {
                target = 60.00;
            } else if (item.equals("60-69分")) {
                target = 70.00;
            } else if (item.equals("70-79分")) {
                target = 80.00;
            } else if (item.equals("80-89分")) {
                target = 90.00;
            } else if (item.equals("90分及以上")) {
                target = 100.00;
            }
            values.add(getCount(items, target));
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    @RequestMapping("/getItemsByChinaHistory")
    @ResponseBody
    public Result getItemsByChinaHistory() {
        List<Double> items = scoreMapper.getItemsByChinaHistory();
        List<Object> keys = new ArrayList<>();
        List<Object> values = new ArrayList<>();
        String[] arr = {"小于60分", "60-69分", "70-79分", "80-89分", "90分及以上"};
        for (String item : arr) {
            keys.add(item);
            Double target = null;
            if (item.equals("小于60分")) {
                target = 60.00;
            } else if (item.equals("60-69分")) {
                target = 70.00;
            } else if (item.equals("70-79分")) {
                target = 80.00;
            } else if (item.equals("80-89分")) {
                target = 90.00;
            } else if (item.equals("90分及以上")) {
                target = 100.00;
            }
            values.add(getCount(items, target));
        }
        Result result = new Result(ConstantUtil.SUCCESS_CODE, ConstantUtil.SUCCESS_MESSAGE);
        result.setData(new BarView(keys, values));
        return result;
    }

    private Integer getCount(List<Double> items, Double target) {
        int count = 0;
        for (Double ele : items) {
            if (target == 60 && ele < 60) {
                count++;
            } else if (target == 70 && ele >= 60 && ele < 70) {
                count++;
            } else if (target == 80 && ele >= 70 && ele < 80) {
                count++;
            } else if (target == 90 && ele >= 80 && ele < 90) {
                count++;
            } else if (target == 100 && ele >= 90) {
                count++;
            }
        }
        return count;
    }
}
